package projectUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

public class FileUtil {
	
	@SuppressWarnings("resource")
	public static byte[] getBytesFromFile(File file) throws IOException {
	    InputStream is = new FileInputStream(file);

	    // Get the size of the file
	    long length = file.length();

	    // You cannot create an array using a long type.
	    // It needs to be an int type.
	    // Before converting to an int type, check
	    // to ensure that file is not larger than Integer.MAX_VALUE.
	    if (length > Integer.MAX_VALUE) {
	        // File is too large
	    }

	    // Create the byte array to hold the data
	    byte[] bytes = new byte[(int)length];

	    // Read in the bytes
	    int offset = 0;
	    int numRead = 0;
	    while (offset < bytes.length
	           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
	        offset += numRead;
	    }

	    // Ensure all the bytes have been read in
	    if (offset < bytes.length) {
	        throw new IOException("Could not completely read file "+file.getName());
	    }

	    // Close the input stream and return bytes
	    is.close();
	    return bytes;
	}
	
	public static String ReadAllText(String fileName)
	{
		StringBuilder sb = new StringBuilder();
		try{
			FileInputStream fstream = new FileInputStream(fileName);
			DataInputStream inFile = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(inFile));
			String strLine;
			while ((strLine = br.readLine()) != null)   {
				sb.append(strLine + System.getProperty("line.separator"));
			}
			inFile.close();
			fstream.close();
		}
		catch (Exception ex){//Catch exception if any
			System.err.println("Error: " + ex.getMessage());
		}
		return sb.toString();
	}
	
	public static String[] ReadAllLines(String fileName)
	{
		List<String> text = new LinkedList<String>();
		try{
			FileInputStream fstream = new FileInputStream(fileName);
			DataInputStream inFile = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(inFile));
			String strLine;
			while ((strLine = br.readLine()) != null)   {
				text.add(strLine);
			}
			inFile.close();
			fstream.close();
		}
		catch (Exception ex){//Catch exception if any
			System.err.println("Error: " + ex.getMessage());
		}
		return text.toArray(new String[0]);
	}
	
	public static void WriteTextToFile(String fileName, boolean append, String text)
	{
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(fileName, append));
			out.write(text);
		} 
		catch (IOException e) 
		{ 
		}
		finally
		{
			try
			{
				out.close();
			}
			catch (Exception ex)
			{}
		}

	}
}
